package com.stylefeng.guns.modular.vpn_recommend_mgr.service.impl;

import com.stylefeng.guns.modular.system.model.Recommend;
import com.stylefeng.guns.modular.system.dao.RecommendMapper;
import com.stylefeng.guns.modular.vpn.bo.RecommendDataBo;
import com.stylefeng.guns.modular.vpn.dto.RecommendSearchDTO;
import com.stylefeng.guns.modular.vpn.utils.ExportFileUtil;
import com.stylefeng.guns.modular.vpn.utils.PoiExcelUtil;
import com.stylefeng.guns.modular.vpn.vo.Paging;
import com.stylefeng.guns.modular.vpn_recommend_mgr.service.IRecommendService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-09-09
 */
@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements IRecommendService {
    @Autowired
    private RecommendMapper recommendMapper;

    public Paging queryAllRecommend(RecommendSearchDTO dto) {
        int start = dto.getPage() > 0 ? dto.getPage() - 1 : 0;
        int count = dto.getLimit();
        dto.setStart(start * count);
        dto.setCount(count);
        Paging<RecommendDataBo> paging = new Paging<>();

        Long total = recommendMapper.queryAllCount(dto);
        List<RecommendDataBo> items = recommendMapper.queryRecommendLog(dto);

        paging.setTotal(total);
        paging.setItems(items);

        return paging;
    }

    public void exportExcel(String inviteUserName, String targetUserName, String createTime, String inviteCode, HttpServletResponse response){
        List<RecommendDataBo> dataList = null;
        List<String> headerList = new ArrayList<>();
        String sheetName = "推荐数据";
        RecommendSearchDTO recommendSearchDTO = new RecommendSearchDTO();
        recommendSearchDTO.setInviteUserName(inviteUserName);
        recommendSearchDTO.setTargetUserName(targetUserName);
        recommendSearchDTO.setCreateTime(createTime);
        recommendSearchDTO.setInviteCode(inviteCode);
        recommendSearchDTO.setCount(Integer.MAX_VALUE);
        dataList = baseMapper.queryRecommendLog(recommendSearchDTO);
        headerList.add("邀请用户名");
        headerList.add("被邀请用户名");
        headerList.add("邀请码");
        headerList.add("推荐人使用免费时长");
        headerList.add("推荐人未使用免费时长");
        headerList.add("被邀请人购买vip最后时间");
        headerList.add("被邀请人购买次数");
        headerList.add("被邀请人购买总额");
        headerList.add("注册时间");

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个Excel表单，参数为sheet的名字
        HSSFSheet sheet = workbook.createSheet(sheetName);

        //创建表头
        PoiExcelUtil.setTableHeader(workbook,sheet,headerList);
        //新增数据行，并且设置单元格数据
        for (int i = 0; i < dataList.size(); i++) {
            RecommendDataBo data = dataList.get(i);
            HSSFRow row = sheet.createRow(i + 1);

            row.createCell(0).setCellValue(""+data.getInviteUserName());
            row.createCell(1).setCellValue(""+data.getTargetUserName());
            row.createCell(2).setCellValue(""+data.getInviteCode());
            Integer takeCount = data.getTakeCount() == null? 0: (data.getTakeCount() * 3);
            row.createCell(3).setCellValue(takeCount);
            Integer untakeCount = data.getUntakeCount() == null? 0: (data.getUntakeCount() * 3);
            row.createCell(4).setCellValue(untakeCount);
            row.createCell(5).setCellValue(tranferDate(data.getLastOrderTime()));
            Integer orderCount = data.getOrderCount() == null ? 0: data.getOrderCount();
            row.createCell(6).setCellValue(orderCount);
            Double totalPay = data.getTotalPay() == null ? 0 : data.getTotalPay();
            row.createCell(7).setCellValue(totalPay);
            row.createCell(8).setCellValue(tranferDate(data.getCreateTime()));

        }
        //ExportFileUtil.exportUserInfo(dataList,sheet);


        String fileName = "recommend-"+System.currentTimeMillis()+".xls";
        //写入文件并将文件已二进制流的方式返回为调用者
        PoiExcelUtil.writeWorkBook(response,workbook,fileName);
    }

    public String tranferDate(Date value) {
        if (value == null) return "-";
        DateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        return format.format(value);
    }
}
